package cn.wolfcode.luowowo.wesite.controller;

import cn.wolfcode.luowowo.cache.until.RedisKeys;
import cn.wolfcode.luowowo.common.exception.DisableException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.wesite.until.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by LENOVO on 2019/8/7.
 */
@Controller

public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/hello")
    @ResponseBody
    public Object hello(){

        return  userInfoService.get(1L);
    }

    /**
     *
     * 判断输入的电话是否存在  checkPhone
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){

        return  !userInfoService.checkPhone(phone);//取反因为我们需要返回一个flase不存在才能跳转
    }
    /**
     *
     * 发送验证码的请求 sendVerifyCode
     *
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        AjaxResult result = new AjaxResult();
        userInfoService.sendVerifyCode(phone);
        return  result;//取反因为我们需要返回一个flase不存在才能跳转
    }

    /**
     * 注册功能的实现 userRegist
     */
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword ,String verifyCode){
        AjaxResult result = new AjaxResult();
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
       /*try {
           userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
       }catch (DisableException de){
            de.printStackTrace();
           result=new AjaxResult(false,de.getMessage());
       }catch (Exception e){
            e.printStackTrace();
            result=new AjaxResult(false,"系統繁忙，请联系管理员或者在稍后操作");
       }*/
        return  result;//取反因为我们需要返回一个flase不存在才能跳转
    }

    /**
     * 登录验证
     * userLogin
     */

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletResponse response, String username, String password){

        String token = userInfoService.userLogin(username, password);
        //將token封裝到
       /* Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");//设置为根路径,表示该任何cookie的请求都可以共享
        cookie.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());//设置cookie存活的事件
        response.addCookie(cookie);*/
        CookieUtil.addCookie(response,"token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        return   AjaxResult.SUCCESS;//取反因为我们需要返回一个flase不存在才能跳转
    }



}
